<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :keyboard="false"
    :forceRender="true"
    v-bind:prefixNo="prefixNo"
    switchHelp
    switchFullscreen
    @cancel="handleCancel"
    wrapClassName="ant-modal-cust-warp"
    :id="prefixNo"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button @click="handleCancel">{{$t('inPrint.cancel')}}</a-button>
      <!-- <a-button v-if="isCanCheck" @click="handleOkAndCheck">保存并审核</a-button> -->
      <a-button type="primary" @click="handleReleasePurchaseOk" :disabled="confirmLoading">{{$t('commodityManagement.preservation')}}</a-button>
    </template>
    <div style="position:absolute;bottom:53px;right:0;z-index:11" v-if="showRecover && !model.id">
      <span>{{ $t('popup.recoveryPrompt') }}</span>
      <a-button type="link" @click="recover" :disabled="submitBtnDisabled">{{$t('popup.recover')}}</a-button>
      <a-button type="link" @click="lose" :disabled="submitBtnDisabled">{{$t('popup.lose')}}</a-button>
    </div>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <j-editable-table id="billModal"
          :ref="refKeys[0]"
          :title="title"
          :loading="materialTable.loading"
          :columns="materialTable.columns"
          :dataSource="materialTable.dataSource"
          :minWidth="minWidth"
          :maxHeight="300"
          :rowNumber="false"
          :rowSelection="true"
          :actionButton="false"
          :dragSort="false"
          @inputPurchaseValueChange="onValueChange"

          @added="onAdded"
          @deleted="onDeleted">
        </j-editable-table>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import ManyAccountModal from '../dialog/ManyAccountModal'
  import CustomerModal from '../../system/modules/CustomerModal'
  import AccountModal from '../../system/modules/AccountModal'
  import HistoryBillList from '../dialog/HistoryBillList'
  import { FormTypes } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { BillModalMixin } from '../mixins/BillModalMixin'
  import { getMpListShort,handleIntroJs } from "@/utils/util"
  import JSelectMultiple from '@/components/jeecg/JSelectMultiple'
  import JUpload from '@/components/jeecg/JUpload'
  import JDate from '@/components/jeecg/JDate'
  import Vue from 'vue'
  export default {
    name: "ReleasePurchaseOrderModal",
    mixins: [JEditableTableMixin, BillModalMixin],
    components: {
      ManyAccountModal,
      CustomerModal,
      AccountModal,
      HistoryBillList,
      JUpload,
      JDate,
      JSelectMultiple,
      VNodes: {
        functional: true,
        render: (h, ctx) => ctx.props.vnodes,
      }
    },
    data () {
      return {
        // title:"下达采购订单列表",
        width: '1600px',
        moreStatus: false,
        selectionRows: '',
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        visible: false,
        operTimeStr: '',
        prefixNo: 'CJZF',
        fileList:[],
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        refKeys: ['materialDataTable', ],
        activeKey: 'materialDataTable',
        materialTable: {
          loading: false,
          dataSource: [],
          columns: [
            { title: this.$t('otherJS.goodsId'), key: 'materialId', width: '6%', type: FormTypes.hidden },
            { title: this.$t('setailManagement.warehouseName'), key: 'depotId', width: '7%', type: FormTypes.hidden },
            // { title: '条码', key: 'barCode', width: '8%', type: FormTypes.popupJsh, kind: 'material', multi: true,
            //   validateRules: [{ required: true, message: '${title}不能为空' }]
            // },
            // { title: this.$t('reportQuery.barcode'), key: 'barCode', width: '8%', type: FormTypes.normal },
            { title: this.$t('otherJS.goodsId'), key: 'meId', width: '6%', type: FormTypes.hidden },
            { title: this.$t('reportQuery.supplier'), key: 'organName',readonly:true, width: '20%', type: FormTypes.input, options: [],
              validateRules: [{ required: true, message: '${title}'+this.$t('inPrint.cannotTe') }]
            },
            { title: this.$t('reportQuery.name'), key: 'name', width: '16%', type: FormTypes.normal },
            { title: this.$t('reportQuery.specification'), key: 'standard', width: '16%', type: FormTypes.normal },
            { title: this.$t('reportQuery.category'), key: 'categoryName', width: '6%', type: FormTypes.input,readonly:true  },
            { title: this.$t('reportQuery.model'), key: 'model', width: '5%', type: FormTypes.normal },
            { title: this.$t('reportQuery.color'), key: 'color', width: '5%', type: FormTypes.normal },
            // { title: '扩展信息', key: 'materialOther', width: '5%', type: FormTypes.normal },
            { title: this.$t('reportQuery.inventory'), key: 'stock', width: '5%', type: FormTypes.normal },
            { title: this.$t('reportQuery.unit'), key: 'unit', width: '6%', type: FormTypes.normal },
            // { title: '多属性', key: 'sku', width: '4%', type: FormTypes.normal },
            { title: this.$t('reportQuery.quantity'), key: 'operNumber', width: '6%', type: FormTypes.normal, statistics: true,
              validateRules: [{ required: true, message: '${title}'+this.$t('inPrint.cannotTe') }]
            },
            { title: this.$t('reportQuery.unitPrice'), key: 'unitPrice', width: '5%', type: FormTypes.inputNumber},
            { title: this.$t('reportQuery.taxMoney'), key: 'allPrice', width: '5%', type: FormTypes.inputNumber, statistics: true },
            { title: this.$t('basicInformation.taxRate'), key: 'taxRate', width: '4%', type: FormTypes.inputNumber,placeholder: '%'},
            { title: this.$t('inPrint.taxAmount'), key: 'taxMoney', width: '5%', type: FormTypes.inputNumber, readonly: true, statistics: true },
            { title: this.$t('inPrint.taxUnitPrice'), key: 'taxUnitPrice', width: '5%', type: FormTypes.inputNumber, readonly: true, statistics: true },
            { title: this.$t('inPrint.totalTax'), key: 'taxLastMoney', width: '6%', type: FormTypes.hidden, statistics: true },
            { title: this.$t('reportQuery.remarks'), key: 'remark', width: '5%', type: FormTypes.input },
            { title: '', key: 'priceNote', width: '5%', type: FormTypes.hidden },
            { title: '', key: 'sourceId', width: '0', type: FormTypes.normal },
            
            { title: '', key: 'custom4', width: '0', type: FormTypes.hidden }
          ]
        },
        confirmLoading: false,
        validatorRules:{
          operTime:{
            rules: [
              { required: true, message: this.$t('popup.pleaseEnterDocumentDate')+'!' }
            ]
          },
          organId:{
            rules: [
              { required: true, message: this.$t('popup.pleaseSelectCustomer')+'!' }
            ]
          }
        },
        url: {
          release: '/depotHead/getIssuePurchase',
          add: '/depotHead/addDepotHeadAndDetail',
          edit: '/depotHead/updateDepotHeadAndDetail',
          detailList: '/depotItem/getDetailList'
        }
      }
    },
    created () {
    },
    methods: {
      //调用完edit()方法之后会自动调用此方法
      editAfter() {
        this.arrList = []
        this.getSupplier()
        let arr = []
        this.materialTable.dataSource.forEach(item => {
          arr.push(item.materialId)
        });
        this.materialIds = arr.toString()
        this.changeMaterialIds()
      },
      release(selectionRows) {
        this.selectionRows = selectionRows.id
        let params = {
          headerId: selectionRows.id
        }
        this.requestSubTableData(this.url.release, params, this.materialTable);
      },
      //提交单据时整理成formData
      classifyIntoFormData(allValues) {
        let totalPrice = 0
        let billMain = Object.assign(this.model, allValues.formValue)
        let detailArr = allValues.tablesValue[0].values
        console.log('detailArr :>> ', detailArr);
        billMain.type = '其它'
        billMain.subType = '厂家直发'
        billMain.defaultNumber = billMain.number
        for(let item of detailArr){
          item.depotId = '' //订单不需要仓库
          totalPrice += item.allPrice - 0
          item.custom4 = item.sourceId
        }
        billMain.totalPrice = totalPrice
        if(billMain.accountId === 0) {
          billMain.accountId = ''
        }
        billMain.accountIdList = this.accountIdList.length>0 ? JSON.stringify(this.accountIdList) : ""
        billMain.accountMoneyList = this.accountMoneyList.length>0 ? JSON.stringify(this.accountMoneyList) : ""
        // if(this.fileList && this.fileList.length > 0) {
        //   billMain.fileName = this.fileList
        // } else {
        //   billMain.fileName = ''
        // }
        if(this.model.id){
          billMain.id = this.model.id
        }
        billMain.salesMan = this.personList.value
        billMain.status = this.billStatus
        // let fileList = JSON.stringify(JSON.parse(localStorage.getItem("fileList")))
        return {
          // info: JSON.stringify(billMain),
          // attachment: fileList,
          rows: JSON.stringify(detailArr),
        }
      },
      handleHistoryBillList() {
        let organId = this.form.getFieldValue('organId')
        this.$refs.historyBillListModalForm.show(this.$t('manuManagement.other'), this.$t('manuManagement.salesOrder'), this.$t('manuManagement.customer'), organId);
        this.$refs.historyBillListModalForm.disableSubmit = false;
      },
    }
  }
</script>
<style scoped lang="less">
::v-deep .ant-spin-container .thead {
  background: #beddf9;
  & > .tr > .td {
    border-left: 1px solid #f2f2f2;
    text-align: center;
  }
}
</style>